package com.daydr3am.thenation;

import java.io.File;

import org.w3c.dom.Node;

import com.coe3.data.Information;
import com.daydr3am.lib.ImageLoader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class DontMiss extends SubScroll {
    public DontMiss(Context ctx, AttributeSet attrs) {
		super(ctx, attrs);
		// TODO Auto-generated constructor stub
		
	}

    public void receiveData(){
    	
		for(int i=0;i<2;i++){
			Node currentNode=list.item(i);
			
//			LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//    		View pageView = layoutInflater.inflate(R.layout.dont_miss, null);
//    		this.addPage(pageView);
//    		
//    		TextView groupName = (TextView) findViewById(R.id.dont_section_textview);
//    		TextView titleName = (TextView) pageView.findViewById(R.id.dont_title_textview);
//    		ImageView img = (ImageView) findViewById(R.id.dont_thumb_imageview);
    		
//    		try {
//				Information.logDEBUG("<=== DontMiss receiveData ===>");
//				Information.logDEBUG("i: " + i);
//				
//				for (int j = 0; j < currentNode.getChildNodes().getLength(); j++) {
//					Information.logDEBUG("j: " + j);
//					Information.logDEBUG("getChildNodes: " + currentNode.getChildNodes().item(j).getTextContent());
//				}
//				
//				Information.logDEBUG("currentNode = null: " + (currentNode == null));
//				Information.logDEBUG("1 = null: " + (currentNode.getChildNodes().item(1).getTextContent() == null));
//				Information.logDEBUG("5 = null: " + (currentNode.getChildNodes().item(5).getTextContent() == null));
//				Information.logDEBUG("17 = null: " + (currentNode.getChildNodes().item(17).getTextContent() == null));
//	    		Information.logDEBUG("1: " + currentNode.getChildNodes().item(1).getTextContent());
//	    		Information.logDEBUG("5: " + currentNode.getChildNodes().item(5).getTextContent());
//	    		Information.logDEBUG("17: " + currentNode.getChildNodes().item(17).getTextContent());
//			} catch (Exception e) {
//				// TODO: handle exception
//				Log.e("chud", "DontMiss Error: " + e.getMessage());
//			}
    		
//    		groupName.setText(currentNode.getChildNodes().item(1).getTextContent());
//    		titleName.setText(currentNode.getChildNodes().item(5).getTextContent());
//    		
////    		ImageLoader imgLoader = new ImageLoader(con.getApplicationContext());
////    		imgLoader.DisplayImage(currentNode.getChildNodes().item(17).getTextContent(), 0, img);
//    		Information.logDEBUG("DontMiss img url: " + currentNode.getChildNodes().item(17).getTextContent());
//    		if(Information.isOnline(getContext())){
//        		ImageLoader imgLoader = new ImageLoader(con.getApplicationContext());
//        		imgLoader.DisplayImage(currentNode.getChildNodes().item(17).getTextContent(), 0, img);
//    		} else if(TheNationMain.getIsOffline(getContext())){
//    			File path = new File(TheNationMain.getPathCurrentNew(getContext()), currentNode.getChildNodes().item(17).getTextContent());
//    			if(!path.exists())
//    				path = new File(TheNationMain.getPathCurrentNew(getContext()), "default.gif");
//    				
//    			Bitmap mBitmap = null;
//				mBitmap = BitmapFactory.decodeFile(path.getAbsolutePath());
//				img.setImageBitmap(mBitmap);
//			}//end if
			
			if(currentNode.getChildNodes().getLength()>5){
		        LayoutInflater layoutInflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        		View pageView = layoutInflater.inflate(R.layout.dont_miss, null);
        		this.addPage(pageView);
        		
        		TextView groupName = (TextView) findViewById(R.id.dont_section_textview);
        		TextView titleName = (TextView) pageView.findViewById(R.id.dont_title_textview);
        		ImageView img = (ImageView) findViewById(R.id.dont_thumb_imageview);
        		
        		groupName.setText(currentNode.getChildNodes().item(1).getTextContent());
        		titleName.setText(currentNode.getChildNodes().item(5).getTextContent());
        		
//        		ImageLoader imgLoader = new ImageLoader(con.getApplicationContext());
//        		imgLoader.DisplayImage(currentNode.getChildNodes().item(17).getTextContent(), 0, img);
        		Information.logDEBUG("DontMiss img url: " + currentNode.getChildNodes().item(17).getTextContent());
        		
        		if(TheNationMain.getIsOffline(getContext())){
        			File path = new File(TheNationMain.getPathCurrentNew(getContext()), currentNode.getChildNodes().item(17).getTextContent());
        			if(!path.exists())
        				path = new File(TheNationMain.getPathCurrentNew(getContext()), "default.gif");
        				
        			Bitmap mBitmap = null;
    				mBitmap = BitmapFactory.decodeFile(path.getAbsolutePath());
    				img.setImageBitmap(mBitmap);
				} else {
					if(Information.isOnline(getContext())){
		        		ImageLoader imgLoader = new ImageLoader(con.getApplicationContext());
		        		imgLoader.DisplayImage(currentNode.getChildNodes().item(17).getTextContent(), 0, img);
	        		}//end if
				}//end if
			}
		}
    }
    
    public boolean onTouch(View v, MotionEvent event) {
    	boolean value= super.onTouch(v, event);
		if(event.getAction()==MotionEvent.ACTION_UP&&!isMove){
			int page=getCurrentPage();
			TheNationMain main=(TheNationMain) getContext();
			main.clickNews(list.item(page*2+1));
		}
		return value;
	}
}
